package cn.gson.crm.controller.relation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.dao.MaintenanceRecordDao;
import cn.gson.crm.model.entity.WhMaintenanceRecord;
import cn.gson.crm.model.entity.WhNewTechnology;
import cn.gson.crm.model.mapper.MaintenanceRecordMapper;

/**
 * 
 * @author GHOST
 *
 */
@Controller
@RequestMapping("/maintenance")
public class MaintenanceRecordController {

	@Autowired
	private MaintenanceRecordMapper mrm;
	
	@Autowired
	private MaintenanceRecordDao mrd;
	
	/**
	 * 查询所有维护记录
	 * @param stu
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> index(WhMaintenanceRecord stu, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize){
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<WhMaintenanceRecord> pageData = mrm.list(stu);

		PageInfo<WhMaintenanceRecord> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	/**
	 * 删除维护状态
	 * @param id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String,Object> delete(@PathVariable("id") Long id) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		WhMaintenanceRecord mr;
		if (id != null && (mr = mrd.findOne(id)) != null) {
			mr.setDelete(0L);
			obj.put("success", true);
		} else {
			obj.put("message", "抱歉，没有找到该条记录！");
		}
		return obj;
	}
	
	/**
	 * 将维护记录信息放在添加维护维护表单上
	 * @param model
	 * @return
	 */
	@GetMapping({ "add_maintenance_record", "add_maintenance_record/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			//根据Id查询就业学员的反馈信息
			WhMaintenanceRecord wmr = mrd.findOne(id);
			//查询所有就业学员的姓名和id
			if (wmr != null) {
				// 如果是编辑，则把原有用户信息传回表单页面
				model.addAttribute("wmr", wmr);
			}
		}
		List<Map<String, Object>> stu = mrm.selectAllStudent();
		List<Map<String, Object>> emp = mrm.selectAllEmp();
		model.addAttribute("student", stu);
		model.addAttribute("emp", emp);
		return "maintenance/add_maintenance_record";
	}
	
	
	
	/**
	 * 添加和编辑
	 * @param stuinfor
	 * @param br
	 * @return
	 */
	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String,Object> save(
			@Valid WhMaintenanceRecord wmr, BindingResult br) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("erros", br.getFieldErrors());
		} else {
			if (wmr.getStudent().getId() == null) {	
					obj.put("message", "该用户不存在！");
					return obj;
					
			} else {
				wmr.setDelete(1L);
				mrd.save(wmr);
				obj.put("success", true);
			}
			
		}
		return obj;
	}
	
	/**
	 * 查看详情
	 * @param id
	 * @param wsi
	 * @param model
	 * @return
	 */
	@RequestMapping({"/maintenance_content_detail","/maintenance_content_detail/{id}"})
	public String indexs(@PathVariable(name = "id", required = false) Long id,WhMaintenanceRecord wmr,Model model){
		if(id != null) {
			wmr=mrd.findOne(id);
			String job =  wmr.getEmp().getJob();
			String emp = wmr.getEmp().getName();
			String stu = wmr.getStudent().getName();
			 
			 
			if (wmr != null ) {		
				model.addAttribute("wmr", wmr);
				model.addAttribute("job", job);
				model.addAttribute("emp", emp);
				model.addAttribute("stu", stu);
			}
		}
		return "maintenance/maintenance_content_detail";
	
	}	
}
